package com.eugene.shiftscalendar;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.preference.PreferenceManager;

import java.util.Calendar;

/**
 * Created by Eugene on 02.04.14.
 */
public class ChinesePainter extends BasePainter{

    public ChinesePainter(WidgetSizes sizes)
    {
        super(sizes);
    }

    int mainLayout()
    {
        return R.layout.chinese;
    }

    protected Point getSize(WidgetSizes.WidgetType type)
    {
        return _sizes.getSize(type, 2);
    }

    protected void paintTitle(Canvas canvas, Calendar cal)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Title);
        int szX = sizes.x;
        int szY = sizes.y;

        // background
        Paint grad = new Paint();
        grad.setAntiAlias(true);
        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(80, 0, 0, 0), Color.argb(150,0,0,0), Shader.TileMode.MIRROR));
        canvas.drawRect(new Rect(0,0,szX,szY), grad);

        // text
        Typeface flow = Typeface.createFromAsset(_context.getAssets(), "fonts/basic_comical_regular_nc.ttf");
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setSubpixelText(true);
        textPaint.setColor(Color.argb(255, 232, 232, 232));
        textPaint.setTypeface(flow);
        textPaint.setTextSize(szY*5/9);
        Rect bounds = new Rect();
        textPaint.getTextBounds("M",0,1,bounds);
        String[] months = {"Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентябрь","Октябрь","Ноябрь","Декабрь"};
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        canvas.drawText(months[month]+" "+year, szY/4*3/2, szY/2 + bounds.height()/2, textPaint);
    }

    protected void paintPrev(Canvas canvas)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Prev);
        int szX = sizes.x;
        int szY = sizes.y;

        // background
        Paint grad = new Paint();
        grad.setAntiAlias(true);
        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(80, 0, 0, 0), Color.argb(150,0,0,0), Shader.TileMode.MIRROR));
        canvas.drawRect(new Rect(0,0,szX,szY), grad);

        // Image
        int heightPercent = 50;
        Bitmap arrow = BitmapFactory.decodeResource(_context.getResources(), R.drawable.prev_month_1);
        int heightNew = szY*heightPercent/100;
        double scale = (double)arrow.getHeight() / heightNew;
        int widthNew = (int)(arrow.getWidth()/scale);
        canvas.drawBitmap(arrow,new Rect(0,0,arrow.getWidth(),arrow.getHeight()),
                new Rect(szX/2-widthNew/2,(50-heightPercent/2)*szY/100,szX/2+widthNew/2,(50+heightPercent/2)*szY/100),
                new Paint(Paint.FILTER_BITMAP_FLAG + Paint.ANTI_ALIAS_FLAG + Paint.DITHER_FLAG));
    }

    protected void paintNext(Canvas canvas)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Next);
        int szX = sizes.x;
        int szY = sizes.y;

        // background
        Paint grad = new Paint();
        grad.setAntiAlias(true);
        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(80, 0, 0, 0), Color.argb(150,0,0,0), Shader.TileMode.MIRROR));
        canvas.drawRect(new Rect(0,0,szX,szY), grad);

        // Image
        int heightPercent = 50;
        Bitmap arrow = BitmapFactory.decodeResource(_context.getResources(),R.drawable.next_month_1);
        int heightNew = szY*heightPercent/100;
        double scale = (double)arrow.getHeight() / heightNew;
        int widthNew = (int)(arrow.getWidth()/scale);
        canvas.drawBitmap(arrow,new Rect(0,0,arrow.getWidth(),arrow.getHeight()),
                new Rect(szX/2-widthNew/2,(50-heightPercent/2)*szY/100,szX/2+widthNew/2,(50+heightPercent/2)*szY/100),
                new Paint(Paint.FILTER_BITMAP_FLAG + Paint.ANTI_ALIAS_FLAG + Paint.DITHER_FLAG));
    }

    protected void paintSettings(Canvas canvas)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Settings);
        int szX = sizes.x;
        int szY = sizes.y;

        // background
        Paint grad = new Paint();
        grad.setAntiAlias(true);
        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(80, 0, 0, 0), Color.argb(150,0,0,0), Shader.TileMode.MIRROR));
        canvas.drawRect(new Rect(0,0,szX,szY), grad);

        // Image
        int heightPercent = 50;
        Bitmap arrow = BitmapFactory.decodeResource(_context.getResources(),R.drawable.settings_1);
        int heightNew = szY*heightPercent/100;
        double scale = (double)arrow.getHeight() / heightNew;
        int widthNew = (int)(arrow.getWidth()/scale);
        canvas.drawBitmap(arrow,new Rect(0,0,arrow.getWidth(),arrow.getHeight()),
                new Rect(szX/2-widthNew/2,(50-heightPercent/2)*szY/100,szX/2+widthNew/2,(50+heightPercent/2)*szY/100),
                new Paint(Paint.FILTER_BITMAP_FLAG + Paint.ANTI_ALIAS_FLAG + Paint.DITHER_FLAG));
    }

    protected void paintWeeks(Canvas canvas)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Weeks);
        int szX = sizes.x;
        int szY = sizes.y;
        //init
        int YOffset = szY/2;
        int LeftOffset = getSize(WidgetSizes.WidgetType.LeftSide).x;
        int RightOffset = getSize(WidgetSizes.WidgetType.RightSide).x;
        int XSize = szX - LeftOffset - RightOffset;
        String days[] = {"Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс"};

        // background

        // text
        Typeface flow = Typeface.createFromAsset(_context.getAssets(), "fonts/basic_comical_regular_nc.ttf");
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setSubpixelText(true);
        textPaint.setColor(Color.argb(255,232,232,232));
        textPaint.setTextSize(szY-YOffset);
        textPaint.setTypeface(flow);
        Rect bounds = new Rect();
        textPaint.getTextBounds("M",0,1,bounds);
        for(int i=0;i<7;i++){
            canvas.drawText(days[i], LeftOffset + XSize/14 + i*XSize/7 - textPaint.measureText(days[i])/2, szY/2+bounds.height()/2, textPaint);
        }
    }

    protected void paintDay(Canvas canvas, Calendar cal)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Day);
        int szX = sizes.x;
        int szY = sizes.y;

        // today
        Calendar todayCal = Calendar.getInstance();
        boolean today = todayCal.get(Calendar.YEAR) == cal.get(Calendar.YEAR) && todayCal.get(Calendar.MONTH) == cal.get(Calendar.MONTH) &&
                todayCal.get(Calendar.DAY_OF_MONTH) == cal.get(Calendar.DAY_OF_MONTH);

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(_context);
        int month = sp.getInt("CurrMonth",cal.get(Calendar.MONTH));

        // background
        Paint line = new Paint();
        line.setAntiAlias(true);
        line.setColor(today && cal.get(Calendar.MONTH) == month ? Color.argb(255,200,200,200) : Color.argb(170,200,200,200));
        if(today && cal.get(Calendar.MONTH) == month) line.setStrokeWidth(5);
        canvas.drawColor(Color.argb(150,0,0,0));
        canvas.drawLine(0,0,0,szY,line);
        canvas.drawLine(0,szY,szX,szY,line);
        canvas.drawLine(szX,szY,szX,0,line);
        canvas.drawLine(szX,0,0,0,line);

        // text
        Typeface flow = Typeface.createFromAsset(_context.getAssets(), "fonts/basic_comical_regular_nc.ttf");
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setSubpixelText(true);
        if(cal.get(Calendar.MONTH) != month){
            textPaint.setColor(_dateType.isHoliday(cal,_context) ? Color.argb(200, 150, 150, 150) : Color.argb(200, 150, 150, 150));
        }
        else{
            textPaint.setColor(_dateType.isHoliday(cal,_context) ? Color.argb(255, 232, 32, 32) : Color.argb(255, 232, 232, 232));
        }
        textPaint.setTextSize(szY*1/3);
        textPaint.setTypeface(flow);
        Rect bounds = new Rect();
        String text = "" + cal.get(Calendar.DAY_OF_MONTH);
        textPaint.getTextBounds(text,0,text.length(), bounds);
        canvas.drawText(text, szX/2 - bounds.width()/2 - szX/5, szY/2 + bounds.height()/2 - szY/5, textPaint);

        if(_dateType.isUserChanged(cal, _context) && cal.get(Calendar.MONTH) == month){
            Paint grad = new Paint();
            grad.setAntiAlias(true);
            grad.setColor(Color.argb(255,232,232,232));
            Rect corner = new Rect((int)(szX - szX*0.2 - 2), 2, szX-2, (int)(2 + szY*0.2));

            Path path = new Path();
            path.moveTo(corner.left, corner.top);
            path.lineTo(corner.right, corner.top);
            path.lineTo(corner.right, corner.bottom);
            path.lineTo(corner.left, corner.top);

            canvas.drawPath(path, grad);
        }
    }

    protected void paintFooter(Canvas canvas){}
    protected void paintLeftSide(Canvas cnv){}
    protected void paintRightSide(Canvas cnv){}
    protected void paintUpper(Canvas cnv){}
}
